//Jeff Szcinski & KQBright
//Lab 1
//ECEN 4243
//Spring 2014
//http://lgjohn.ecen.ceat.okstate.edu/4243/proj/alu32.txt

module Alu_cell (d, g, p, a, b, c, S);
	output d, g, p;
	input a, b, c;
	input [2:0] S;
	
	reg bint, cint, g, p, d;
		
	always@(a,b,c,g,p,S)begin
		bint = S[0] ^ b; //subtraction functionality
		cint = S[1] & c; //when s[1] = 0, XOR and XNOR functionality
		
		//add cell module with g,p,d
		g = a & bint;
		p = a ^ bint;
		if(S[2] == 0)begin
			d = p ^ cint;
		end
		else if (S[2] == 1)begin
			if(S[1] == 0)begin
				if(S[0] == 0)begin
					d = a | b;
				end
				else if(S[0] == 1)begin
					d = ~(a | b);
				end
			end
			if(S[1] == 1)begin
				if(S[0] == 0)begin
					d = a & b;
				end
				else if(S[0] == 1)begin
					d = 0;
				end
			end
		end
	end
endmodule